package br.com.webselector.dao;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import br.com.webselector.util.JPAUtil;

public abstract class JPABasicDao <T> {
	
	@SuppressWarnings("hiding")
	public <T> T save(T objToSave){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		em.persist(objToSave);
		em.getTransaction().commit();
		em.close();
		return objToSave;
	}
	
	public void remove(Class<T> objetToRemote, Serializable key){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		T obj = em.find(objetToRemote, key);
		if(obj != null){
			em.remove(obj);
		}
		em.getTransaction().commit();
		em.close();
	}
	
	
	/*
	public void update(Class<T> classToCast, Serializable key, Object... values ){
		EntityManager em = JPAUtil.getInstance().createEntityManager();	
		T obj = em.find(classToCast, key);
		if(values != null){
			obj = obj.
				query.setParameter(i+1,values[i]);
			
		}
		em.merge(obj);
		em.getTransaction().commit();
		em.close();
	}*/
	
	/*
	public void update(T Object){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		em.merge(Object);
		em.getTransaction().commit();
		em.close();
	}*/
	
	public T reflesh (T objToRefresh){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		em.refresh(objToRefresh);
		em.getTransaction().commit();
		em.close();
		return objToRefresh;
	}
	
	@SuppressWarnings("hiding")
	public <T> T FindByPrimaryKey(Class<T> classToCast, Serializable key){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		T obj = em.find(classToCast, key);
		em.getTransaction().commit();
		em.close();
		return obj;
	}
	
	public int executeCommand(String ejbql, Object... values){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		Query query = em.createQuery(ejbql);
		if(values != null){
			for (int i = 0; i < values.length; i++) {
				query.setParameter(i+1,values[i]);
			}
		}
		int toReturn = query.executeUpdate();
		em.getTransaction().commit();
		em.close();
		return toReturn;
	}
	
	@SuppressWarnings("hiding")
	public <T> T getPurePojo(Class<T> classToCast, String ejbl, Object... values){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		Query query = em.createQuery(ejbl);
		if(values != null){
			for(int i = 0; i < values.length; i++){
				query.setParameter(i+1, values[i]);
			}
		}
		@SuppressWarnings("unchecked")
		T entityToReturn = (T) query.getSingleResult();
		em.getTransaction().commit();
		em.close();
		return entityToReturn;
	}
	
	@SuppressWarnings("hiding")
	public <T> List <T> getPureList(Class<T> classToCast, String ejbl, Object... values){
		EntityManager em = JPAUtil.getInstance().createEntityManager();
		Query query = em.createQuery(ejbl);
		if(values != null){
			for(int i = 0; i < values.length; i++){
				query.setParameter(i+1,values[i]);
			}
		}
		@SuppressWarnings("unchecked")
		List<T> listReturn = query.getResultList();
		em.getTransaction().commit();
		em.close();
		return listReturn;
	}
	
	
	/*
	protected int getNextValueOfId(String className, String column){
		Integer toReturn = getPurePojo(Integer.class,"SELECT MAX(pref.?2) from ?1 pref",className, column);
		if(toReturn == null){
			return 1;
		} else {
			return toReturn.intValue() + 1;
		}
	}
	
	protected abstract void setNextValueOfId(T object);*/

}
